#include "mcp_tools.h"
#include "board.h"
#include <driver/uart.h>
#include <esp_log.h>
#include <map>
#include <algorithm>

#define TAG "InfraredTool"

static std::map<std::string, std::string> RoomLightMap = {
    {"turnOn", "8914,-4623,442,-660,459,-660,461,-660,459,-660,459,-661,459,-661,459,-660,461,-660,461,-1784,442,-1784,441,-1785,442,-1784,440,-1785,442,-1784,443,-1784,440,-1785,442,-661,459,-661,459,-661,459,-662,461,-660,461,-660,462,-1784,442,-660,461,-1784,441,-1785,441,-1785,442,-1785,442,-1784,441,-1785,442,-660,461,-1785,441,-40046,8910,-2365,443,-60638,len=72"},
    {"turnOff", "8927,-4630,442,-659,463,-659,463,-659,462,-660,462,-660,463,-659,463,-660,461,-661,461,-1788,441,-1786,440,-1787,439,-1788,441,-1787,440,-1787,440,-1788,439,-1789,441,-660,459,-662,459,-662,461,-661,462,-660,461,-661,459,-1788,439,-662,461,-1787,440,-1788,440,-1787,439,-1788,440,-1789,439,-1788,440,-661,462,-1786,440,-40044,8929,-2368,439,-60638,len=72"}
};
static std::map<std::string, std::string> AirBtnMap = {
    {"powerOn", "5970,-7521,412,-718,382,-719,401,-718,405,-718,406,-717,406,-717,381,-720,403,-718,405,-718,406,-717,406,-717,381,-718,403,-719,405,-718,405,-717,408,-718,379,-719,403,-697,427,-717,409,-716,408,-694,402,-719,404,-717,406,-717,409,-717,382,-719,403,-718,405,-717,408,-692,432,-716,408,-693,403,-696,428,-692,434,-690,434,-714,385,-719,403,-715,409,-712,413,-713,411,-713,386,-720,403,-716,408,-694,431,-713,413,-691,406,-718,403,-715,411,-691,434,-711,412,-713,386,-720,403,-715,410,-713,412,-711,414,-694,403,-700,400,-738,410,-1779,381,-1805,402,-718,382,-737,411,-1778,404,-1781,402,-717,408,-711,388,-735,413,-711,413,-694,381,-739,409,-711,413,-712,414,-1769,411,-712,414,-710,406,-700,424,-1776,408,-698,425,-1760,422,-698,426,-692,433,-1756,426,-689,436,-1754,427,-688,436,-689,436,-1746,436,-687,437,-1746,436,-1749,434,-1752,430,-690,434,-687,438,-1745,437,-1750,432,-688,436,-1748,437,-7521,433,-60612,len=198"},
    {"powerOff", "5962,-7544,388,-699,428,-692,432,-688,437,-689,408,-716,409,-698,428,-689,436,-687,437,-688,410,-717,409,-694,430,-690,435,-687,438,-685,410,-717,408,-694,432,-689,436,-686,438,-686,409,-717,410,-693,434,-685,438,-686,437,-689,408,-699,424,-695,430,-689,436,-689,435,-689,405,-718,409,-693,430,-688,436,-690,435,-690,406,-717,406,-696,430,-690,434,-691,434,-691,405,-718,406,-696,427,-693,432,-691,434,-691,405,-719,405,-696,428,-716,408,-717,408,-715,384,-717,404,-718,404,-717,408,-717,408,-717,406,-694,403,-718,404,-719,404,-718,406,-1771,411,-718,405,-718,379,-1799,411,-1771,410,-720,403,-720,378,-721,400,-722,403,-721,403,-720,402,-721,376,-722,401,-1785,399,-721,377,-745,424,-701,425,-1752,404,-726,423,-1754,405,-724,423,-700,423,-1752,431,-701,423,-1752,430,-701,397,-726,374,-1802,405,-724,376,-1801,408,-1775,409,-722,401,-697,403,-720,404,-1779,403,-1783,401,-722,403,-1781,402,-7555,428,-60612,len=198"},
    {"32", "5968,-7517,389,-698,429,-692,432,-688,438,-687,408,-698,429,-690,436,-688,438,-689,408,-696,431,-689,436,-688,438,-686,410,-695,432,-687,436,-689,410,-716,411,-692,432,-687,438,-687,437,-688,410,-694,432,-687,439,-686,409,-718,408,-695,431,-687,438,-687,409,-718,408,-695,431,-687,438,-687,409,-717,412,-692,432,-687,438,-687,410,-717,410,-692,432,-687,438,-687,410,-717,409,-693,434,-1773,409,-688,439,-686,439,-687,409,-697,429,-688,438,-688,438,-685,386,-720,430,-685,440,-687,435,-689,409,-697,431,-688,436,-687,436,-688,410,-1796,388,-698,429,-1777,404,-695,431,-708,418,-707,389,-716,410,-694,430,-689,438,-708,389,-715,411,-714,411,-709,416,-688,432,-1771,418,-707,388,-698,448,-690,437,-688,436,-1747,437,-1748,436,-687,436,-690,386,-1798,409,-680,448,-1755,428,-691,436,-685,440,-1746,438,-686,440,-1744,440,-1744,440,-685,412,-694,431,-691,437,-683,441,-1746,441,-1745,440,-683,442,-7518,442,-60612,len=198"},
    {"31", "5973,-7504,408,-690,436,-691,434,-691,406,-699,429,-691,437,-689,436,-691,408,-695,430,-691,436,-690,435,-691,408,-695,430,-691,437,-690,436,-691,406,-696,431,-688,436,-690,434,-693,408,-693,432,-690,435,-691,434,-691,408,-695,430,-690,436,-689,435,-693,408,-693,431,-689,437,-690,435,-690,408,-695,430,-690,437,-689,436,-691,408,-694,432,-689,436,-690,435,-691,408,-695,432,-688,436,-1770,416,-689,435,-690,408,-697,429,-692,434,-690,436,-690,406,-699,427,-690,436,-689,436,-690,408,-699,426,-693,434,-690,436,-690,408,-699,428,-1779,406,-694,431,-1776,408,-691,436,-690,436,-690,406,-699,428,-690,436,-689,436,-690,408,-698,429,-689,437,-689,437,-688,408,-1799,387,-1797,390,-1796,435,-1749,436,-1749,435,-667,433,-1775,436,-687,410,-677,449,-1758,428,-670,453,-1756,429,-667,460,-686,439,-1746,438,-686,411,-1774,440,-650,449,-690,435,-686,439,-686,439,-668,431,-1755,428,-1773,415,-693,431,-7526,439,-60612,len=198"},
    {"30", "5955,-7521,389,-699,424,-696,430,-692,434,-691,406,-700,425,-692,432,-693,434,-690,405,-701,426,-692,431,-693,434,-691,406,-700,426,-693,432,-691,435,-691,405,-700,426,-691,434,-693,432,-692,405,-701,425,-692,435,-691,434,-691,405,-701,425,-692,432,-692,435,-691,404,-701,426,-691,434,-691,434,-691,406,-700,425,-692,434,-690,436,-690,406,-700,425,-693,432,-691,434,-692,405,-700,425,-1779,404,-694,432,-690,435,-691,432,-694,406,-694,431,-691,434,-691,435,-691,405,-697,430,-690,435,-691,434,-691,405,-697,430,-691,434,-691,435,-1768,415,-691,405,-1797,414,-693,406,-696,429,-690,436,-689,435,-690,406,-697,429,-690,435,-690,435,-691,406,-699,428,-689,436,-1772,410,-691,434,-1770,434,-1750,432,-1752,432,-667,458,-1747,435,-667,458,-668,429,-1776,408,-677,448,-1758,425,-675,452,-687,438,-1748,436,-665,458,-1745,439,-1746,439,-1747,437,-1749,435,-1750,433,-1755,431,-687,437,-1747,436,-685,440,-7492,416,-60612,len=198"},
    {"29", "5957,-7518,417,-689,408,-699,426,-692,434,-689,436,-691,406,-698,429,-693,434,-689,436,-690,406,-698,429,-692,434,-689,436,-688,409,-699,428,-690,435,-689,437,-689,384,-724,426,-692,434,-688,437,-689,409,-699,426,-692,434,-688,437,-690,405,-701,426,-692,434,-689,438,-687,406,-698,430,-692,434,-689,436,-688,408,-701,426,-691,435,-689,437,-687,408,-698,430,-691,434,-688,437,-689,409,-1796,389,-718,408,-695,431,-688,437,-688,409,-717,408,-695,431,-688,437,-689,409,-716,411,-694,431,-686,438,-688,434,-692,406,-697,430,-689,437,-1770,414,-688,436,-1769,416,-689,437,-689,408,-695,431,-689,437,-688,437,-688,409,-696,430,-688,436,-688,438,-688,408,-698,429,-1777,406,-1795,388,-697,449,-1756,429,-1757,427,-673,453,-1754,431,-670,455,-688,437,-1748,438,-666,432,-1772,439,-668,432,-674,452,-1753,430,-689,438,-1751,434,-687,439,-1745,441,-1747,438,-1749,436,-1750,434,-667,458,-1747,438,-683,441,-7519,442,-60612,len=198"},
    {"28", "5930,-7529,436,-688,438,-686,409,-718,410,-691,434,-687,440,-686,406,-699,430,-687,437,-688,439,-687,409,-697,429,-689,438,-687,439,-686,408,-699,429,-690,436,-688,436,-689,409,-696,430,-688,438,-687,436,-689,384,-721,429,-688,437,-687,438,-688,386,-719,431,-686,439,-687,436,-688,385,-720,431,-686,438,-686,438,-687,408,-699,429,-688,439,-686,438,-686,408,-699,431,-687,438,-685,439,-1766,419,-684,410,-700,426,-693,434,-686,438,-685,411,-716,411,-690,436,-685,440,-686,434,-691,411,-691,435,-684,440,-687,437,-688,410,-692,435,-1775,410,-687,437,-1769,415,-707,417,-708,389,-717,411,-690,436,-706,419,-705,388,-718,410,-710,414,-709,417,-707,390,-717,411,-1774,408,-695,430,-708,436,-1749,438,-1750,432,-687,438,-1745,438,-686,439,-686,411,-1773,409,-677,451,-1771,411,-694,436,-683,440,-1744,442,-683,438,-1745,441,-1743,440,-684,414,-1770,443,-1740,442,-1741,443,-683,414,-1768,443,-683,389,-7548,436,-60612,len=198"},
    {"27", "5965,-7504,406,-694,430,-691,434,-692,434,-692,406,-695,431,-691,434,-691,406,-719,406,-694,430,-692,435,-691,406,-700,425,-695,430,-690,436,-690,408,-700,425,-693,431,-691,435,-691,431,-694,406,-695,431,-689,436,-689,434,-694,406,-693,431,-691,434,-692,434,-691,408,-693,431,-690,436,-689,435,-692,408,-694,430,-689,435,-690,435,-691,408,-696,428,-690,436,-690,436,-689,408,-696,429,-1777,406,-692,432,-690,436,-690,408,-699,425,-693,434,-689,437,-689,409,-700,423,-694,434,-690,436,-689,410,-717,406,-694,432,-688,436,-690,410,-1796,415,-690,406,-1798,388,-699,426,-694,431,-690,435,-690,408,-719,406,-695,430,-691,435,-691,408,-717,406,-697,429,-689,436,-1771,413,-1773,413,-1777,425,-673,453,-1752,433,-670,456,-1749,436,-668,454,-673,427,-1777,408,-676,451,-1757,426,-673,454,-667,458,-1747,437,-668,457,-1746,438,-667,431,-679,448,-1756,429,-1755,429,-1773,413,-676,449,-1754,429,-671,456,-7521,439,-60612,len=198"},
    {"26", "5954,-7526,438,-681,444,-681,362,-764,443,-659,466,-653,445,-681,362,-763,444,-658,466,-656,441,-684,362,-763,445,-658,466,-653,444,-681,362,-764,443,-658,468,-676,448,-654,364,-762,420,-683,467,-677,447,-653,364,-762,419,-685,464,-655,470,-656,409,-716,418,-686,465,-657,467,-655,432,-693,417,-688,463,-678,445,-679,410,-691,362,-766,442,-658,466,-678,412,-691,361,-765,443,-681,443,-1741,444,-654,471,-655,362,-761,420,-686,465,-654,471,-655,436,-691,388,-717,463,-656,469,-654,441,-684,389,-717,463,-660,466,-654,443,-681,362,-1843,387,-717,419,-1787,340,-765,441,-662,464,-656,468,-655,363,-763,419,-684,466,-654,470,-657,408,-716,417,-687,463,-657,468,-1737,446,-677,446,-1735,469,-636,454,-1749,441,-663,408,-1795,408,-694,439,-689,463,-1720,413,-687,487,-1717,465,-658,466,-635,432,-1771,449,-655,385,-1819,410,-1773,445,-1737,449,-656,435,-1769,441,-1743,449,-656,407,-1796,415,-689,413,-7543,440,-60612,len=198"},
    {"25", "5974,-7520,382,-744,403,-719,404,-719,403,-719,377,-721,401,-721,403,-719,404,-720,402,-720,402,-721,352,-744,381,-743,402,-721,404,-718,403,-719,379,-719,378,-744,403,-720,404,-719,404,-719,403,-719,376,-722,387,-735,381,-742,404,-720,403,-719,404,-695,378,-744,402,-719,403,-720,404,-719,402,-720,377,-721,378,-743,403,-720,403,-719,404,-719,403,-719,354,-744,378,-743,379,-744,404,-1775,404,-720,403,-720,403,-718,404,-719,377,-720,378,-743,381,-743,403,-718,405,-717,381,-745,352,-743,403,-717,406,-694,430,-715,409,-715,383,-1799,409,-1771,411,-712,411,-711,413,-1771,410,-1772,410,-712,412,-711,412,-696,400,-722,401,-717,386,-734,411,-713,411,-712,411,-1771,411,-1770,387,-735,432,-691,431,-1751,430,-693,431,-1751,432,-690,409,-715,431,-1750,433,-689,433,-1748,434,-689,434,-673,424,-1775,409,-696,425,-1774,434,-689,409,-1772,435,-1746,437,-687,438,-1744,435,-1750,431,-687,437,-1750,433,-7524,428,-60612,len=198"},
    {"24", "6004,-7495,365,-740,417,-687,437,-682,470,-656,386,-723,431,-690,440,-679,448,-681,362,-763,418,-688,438,-680,446,-681,385,-739,416,-689,440,-681,469,-656,385,-741,444,-661,439,-703,423,-680,410,-717,419,-686,462,-657,446,-678,412,-717,418,-688,462,-655,471,-654,412,-717,414,-693,463,-654,472,-656,408,-717,390,-716,465,-681,421,-680,439,-686,414,-712,445,-680,444,-656,443,-683,361,-1846,387,-716,420,-687,462,-659,444,-703,387,-716,419,-688,436,-682,468,-657,432,-692,417,-710,440,-660,466,-658,435,-688,364,-744,463,-681,445,-1741,440,-680,447,-1741,419,-681,441,-684,389,-737,444,-681,442,-680,446,-656,384,-742,442,-661,465,-677,448,-655,384,-741,414,-1790,365,-740,417,-707,462,-658,441,-1743,442,-661,430,-1775,452,-652,382,-724,481,-1744,413,-670,459,-1748,437,-685,441,-680,441,-1743,445,-681,411,-1773,470,-1714,443,-683,437,-687,416,-1768,390,-1794,413,-692,438,-1766,390,-696,460,-7519,440,-60612,len=198"},
    {"23", "6004,-7503,405,-718,406,-717,409,-717,411,-715,383,-719,408,-696,430,-716,410,-693,405,-719,406,-697,429,-693,434,-693,404,-700,425,-697,430,-693,432,-692,404,-702,423,-698,428,-693,434,-692,405,-701,422,-699,427,-694,434,-692,406,-718,405,-698,429,-692,435,-692,434,-692,406,-696,428,-695,432,-692,434,-692,405,-701,424,-694,432,-692,435,-691,405,-702,425,-693,432,-693,432,-692,405,-1798,415,-691,406,-698,427,-694,432,-693,432,-692,405,-701,425,-694,431,-692,434,-692,408,-701,422,-695,431,-692,434,-713,388,-699,423,-697,429,-1779,405,-692,434,-1777,408,-689,437,-691,435,-690,406,-699,427,-693,434,-691,435,-709,388,-700,425,-693,432,-710,416,-691,431,-1772,414,-1770,415,-1770,433,-1753,432,-691,435,-691,407,-1776,435,-690,408,-678,448,-1759,426,-694,432,-1756,427,-692,435,-689,436,-1747,438,-687,438,-1746,438,-688,410,-695,431,-691,435,-1750,434,-1755,430,-687,438,-1752,431,-688,439,-7519,442,-60612,len=198"},
    {"22", "6013,-7462,447,-706,382,-743,387,-713,443,-681,419,-704,416,-709,387,-713,442,-682,419,-704,443,-681,335,-764,443,-682,442,-681,419,-704,386,-739,385,-716,442,-682,441,-681,415,-710,388,-711,443,-682,419,-705,419,-705,336,-788,386,-714,420,-704,419,-705,386,-738,388,-713,419,-705,418,-705,420,-704,387,-713,394,-731,420,-704,419,-705,384,-742,386,-714,420,-706,417,-704,415,-1769,421,-1766,419,-705,417,-707,358,-740,395,-730,420,-703,420,-704,412,-716,384,-712,418,-707,418,-704,416,-708,356,-743,394,-734,416,-703,420,-705,409,-1772,421,-704,336,-1845,416,-711,334,-762,420,-706,418,-704,420,-704,384,-741,367,-731,419,-705,418,-704,419,-705,336,-760,396,-1813,387,-713,394,-1813,389,-1794,357,-769,388,-711,439,-1742,442,-683,440,-684,440,-1742,439,-683,407,-1776,442,-683,405,-719,389,-1791,360,-767,390,-1791,411,-689,442,-683,442,-680,444,-1739,443,-1741,442,-680,444,-1741,443,-680,414,-7514,441,-60612,len=198"},
    {"21", "5974,-7517,410,-690,434,-690,432,-691,404,-720,405,-696,426,-693,431,-694,430,-690,434,-690,406,-719,404,-699,425,-695,429,-690,434,-691,432,-692,403,-720,404,-699,425,-695,429,-691,432,-690,434,-690,405,-720,404,-699,425,-694,430,-690,432,-692,432,-691,408,-717,402,-702,423,-696,428,-691,432,-691,432,-691,404,-720,403,-700,424,-695,428,-694,431,-691,432,-691,431,-692,404,-1799,412,-1771,411,-690,434,-690,432,-692,381,-726,422,-698,427,-691,431,-693,432,-690,432,-692,403,-702,422,-697,427,-693,430,-692,431,-693,432,-691,404,-1799,411,-692,406,-1797,410,-693,431,-691,405,-720,403,-699,425,-696,428,-690,432,-692,432,-690,408,-717,402,-702,424,-694,429,-1779,402,-1797,385,-717,424,-1779,431,-671,424,-684,440,-1777,433,-670,423,-700,425,-1776,406,-698,426,-1775,434,-690,407,-680,443,-1776,407,-696,427,-1775,434,-1748,434,-1752,429,-1757,424,-677,447,-1774,407,-679,446,-1775,410,-693,430,-7544,437,-60612,len=198"},
    {"20", "6033,-7460,470,-681,335,-788,366,-734,441,-682,417,-704,418,-707,386,-712,394,-730,442,-682,418,-704,409,-716,335,-763,394,-731,441,-681,419,-704,414,-710,388,-710,395,-730,419,-703,420,-705,411,-712,387,-712,419,-706,418,-705,418,-705,383,-741,387,-712,416,-709,418,-703,420,-703,393,-733,335,-762,393,-732,418,-705,419,-705,388,-736,386,-712,419,-705,418,-706,417,-706,384,-1797,420,-1765,417,-706,418,-704,384,-742,385,-713,418,-707,418,-703,419,-705,357,-770,383,-714,417,-709,415,-705,419,-705,383,-741,387,-711,415,-710,417,-1765,417,-709,415,-1765,418,-707,416,-706,418,-705,385,-740,384,-714,419,-706,418,-704,394,-731,387,-737,365,-734,416,-709,393,-1790,414,-709,416,-706,435,-1747,436,-686,415,-709,407,-1773,441,-686,407,-717,386,-1794,408,-718,383,-1796,410,-717,385,-715,439,-1741,412,-713,441,-1741,420,-704,442,-1739,415,-1791,409,-691,446,-1760,409,-690,446,-1759,411,-691,446,-7482,447,-60612,len=198"},
    {"19", "5983,-7515,416,-690,408,-699,426,-694,430,-689,436,-688,435,-690,406,-698,427,-692,431,-690,436,-688,410,-716,406,-698,427,-689,435,-689,435,-689,408,-718,408,-693,430,-687,438,-687,437,-688,405,-718,409,-714,411,-706,418,-685,439,-687,406,-699,426,-692,434,-686,439,-706,415,-687,386,-722,429,-689,436,-704,418,-708,416,-687,409,-717,410,-710,417,-703,418,-706,388,-735,364,-1818,390,-1794,419,-706,388,-717,412,-711,414,-705,417,-705,417,-706,365,-740,411,-712,414,-683,441,-703,417,-707,365,-740,410,-710,416,-705,419,-704,419,-1763,419,-706,388,-1795,421,-706,383,-717,411,-716,411,-706,419,-704,417,-707,388,-717,413,-710,412,-709,417,-702,420,-705,387,-1794,417,-1766,421,-1766,434,-685,438,-685,436,-691,383,-1794,438,-687,385,-719,431,-1770,387,-717,433,-1768,387,-717,434,-685,439,-1747,434,-682,444,-1744,438,-1748,434,-687,436,-1747,436,-683,440,-1749,432,-682,441,-1745,438,-680,443,-7509,441,-60612,len=198"},
    {"18", "5992,-7493,439,-679,470,-656,385,-740,415,-688,438,-683,441,-681,445,-681,385,-723,431,-689,435,-681,443,-683,438,-707,392,-694,434,-688,434,-685,442,-681,437,-687,414,-693,432,-685,442,-682,441,-681,386,-740,414,-692,431,-683,442,-684,437,-686,410,-699,428,-691,434,-685,440,-686,438,-686,385,-739,414,-686,437,-684,440,-683,439,-685,386,-722,431,-689,432,-685,441,-683,437,-1767,416,-1768,417,-685,438,-685,410,-717,410,-693,434,-683,442,-682,439,-686,386,-742,409,-691,432,-689,438,-682,439,-689,405,-700,429,-690,435,-685,439,-1769,413,-685,442,-1767,415,-685,439,-686,410,-713,411,-695,432,-688,435,-708,417,-686,409,-717,410,-695,431,-686,439,-707,416,-1767,417,-708,416,-1767,438,-666,457,-665,430,-678,448,-1773,410,-674,452,-691,434,-1751,430,-689,437,-1749,435,-664,458,-663,463,-1742,440,-683,441,-1741,444,-681,440,-684,410,-1772,445,-681,386,-1795,443,-682,390,-1793,413,-713,414,-7521,411,-60612,len=198"},
    {"17", "6004,-7519,388,-719,404,-717,408,-717,410,-715,409,-693,403,-719,405,-717,408,-716,410,-716,382,-719,405,-717,406,-717,409,-716,410,-692,404,-718,406,-716,409,-716,410,-717,381,-719,405,-717,406,-716,410,-715,409,-717,382,-718,405,-717,409,-715,411,-715,385,-717,404,-717,408,-716,409,-717,408,-716,383,-719,403,-718,406,-716,410,-716,409,-692,406,-718,404,-717,409,-715,409,-1771,413,-1775,409,-715,409,-717,410,-692,404,-718,408,-715,409,-715,411,-714,384,-718,405,-717,406,-715,410,-715,410,-715,382,-719,405,-717,408,-715,409,-1771,412,-715,410,-1770,413,-715,410,-716,382,-719,406,-716,408,-716,409,-716,411,-692,404,-719,406,-715,411,-713,411,-714,383,-1797,413,-1769,414,-716,428,-696,401,-699,427,-696,428,-1757,426,-694,430,-694,432,-1748,436,-692,431,-1748,436,-693,432,-670,427,-1776,408,-681,446,-1774,409,-1774,438,-1746,437,-667,429,-695,430,-1773,410,-676,450,-1754,428,-673,451,-7525,433,-60612,len=198"},
    {"16", "5980,-7500,404,-717,409,-715,409,-716,409,-694,403,-719,405,-715,411,-714,410,-715,383,-718,405,-716,409,-715,410,-714,409,-692,405,-718,406,-715,410,-715,410,-715,382,-718,405,-717,409,-715,409,-716,409,-692,405,-717,406,-716,409,-715,411,-714,383,-718,405,-717,409,-714,410,-715,411,-714,382,-719,405,-715,410,-715,409,-715,385,-717,404,-717,408,-714,411,-692,432,-691,405,-1796,388,-1796,414,-693,403,-719,405,-717,406,-716,410,-714,385,-717,405,-717,406,-716,409,-716,409,-716,383,-701,423,-695,429,-693,431,-692,431,-693,404,-1797,387,-718,405,-1797,387,-699,423,-717,410,-691,432,-691,432,-692,405,-699,425,-715,410,-692,432,-691,408,-700,422,-717,409,-1777,405,-691,432,-692,453,-669,453,-673,425,-678,447,-1757,426,-693,431,-688,436,-1750,432,-669,456,-1747,437,-668,457,-686,437,-1746,437,-688,410,-1773,437,-687,410,-1773,411,-695,429,-692,434,-1753,430,-689,435,-1753,430,-686,439,-7488,441,-60612,len=198"},
    {"cool", "5978,-7525,415,-693,434,-693,404,-699,429,-692,434,-692,435,-692,408,-695,430,-692,435,-693,406,-719,406,-695,430,-693,434,-692,408,-699,425,-694,432,-692,435,-690,406,-699,427,-692,435,-692,432,-693,406,-699,426,-693,432,-692,434,-692,406,-697,429,-692,435,-692,406,-718,406,-697,429,-692,434,-692,432,-676,424,-696,428,-693,435,-691,432,-694,406,-694,432,-692,434,-691,434,-1771,415,-692,406,-697,429,-689,438,-690,435,-692,404,-699,428,-692,434,-690,436,-692,406,-697,429,-710,416,-709,416,-674,424,-696,430,-690,436,-710,416,-1770,416,-691,406,-1799,413,-676,426,-692,432,-711,416,-689,409,-720,405,-714,412,-690,436,-711,388,-719,405,-715,412,-711,416,-1770,414,-1772,413,-712,434,-690,408,-1779,407,-699,427,-1761,423,-696,431,-689,436,-1752,434,-688,437,-1750,437,-688,410,-698,428,-1758,426,-694,432,-1755,431,-1757,428,-691,435,-687,439,-1745,440,-1746,439,-685,441,-1745,440,-686,413,-7534,432,-60612,len=198"},
    {"dry", "5950,-7531,439,-687,437,-689,410,-697,430,-688,440,-687,434,-693,413,-690,434,-687,439,-687,411,-716,411,-692,434,-688,439,-686,409,-699,429,-689,438,-686,440,-685,387,-720,431,-687,439,-686,439,-686,386,-722,431,-687,440,-684,440,-688,409,-695,431,-690,438,-685,412,-715,386,-720,434,-684,441,-684,413,-717,410,-715,412,-686,439,-681,415,-717,413,-690,435,-684,441,-687,409,-1797,388,-717,411,-694,435,-685,438,-687,410,-699,430,-687,437,-688,437,-688,409,-698,431,-687,439,-685,440,-685,411,-697,430,-688,438,-688,437,-709,364,-1820,388,-718,411,-1777,406,-694,432,-1775,411,-1777,409,-690,436,-709,417,-709,388,-697,429,-711,416,-708,416,-710,388,-699,429,-1777,408,-1779,406,-715,432,-688,436,-1749,437,-689,434,-671,432,-1773,409,-699,429,-1756,430,-689,435,-1753,434,-687,439,-688,411,-1773,440,-667,430,-1772,412,-1774,411,-697,431,-688,437,-1750,436,-1750,435,-683,443,-1744,441,-1746,439,-7494,442,-60612,len=198"},
    {"hot", "5964,-7505,410,-692,434,-694,406,-700,426,-693,432,-690,436,-691,406,-699,427,-691,436,-691,434,-692,406,-698,428,-691,435,-691,408,-718,405,-698,430,-689,436,-690,408,-699,426,-693,434,-691,435,-692,405,-698,429,-692,435,-690,434,-692,406,-698,429,-691,435,-691,434,-693,408,-694,430,-691,436,-688,410,-699,427,-714,411,-690,437,-692,405,-699,426,-690,436,-690,436,-689,408,-1799,387,-699,428,-691,435,-692,435,-690,406,-717,409,-692,435,-690,434,-676,425,-715,411,-690,436,-711,415,-691,406,-717,411,-709,416,-710,388,-700,425,-1781,404,-717,410,-1777,408,-711,415,-711,416,-692,404,-717,410,-710,416,-710,388,-719,406,-716,410,-710,416,-711,413,-695,406,-1779,404,-1782,403,-717,429,-690,436,-1752,433,-691,436,-689,408,-697,429,-1757,428,-1758,428,-693,431,-1754,430,-689,437,-688,438,-1747,438,-686,412,-1774,411,-1774,412,-696,430,-692,434,-1752,434,-1752,433,-1755,430,-687,439,-1747,438,-7520,442,-60612,len=198"}
};

namespace mcp_tools {

class InfraredTool : public McpTool {
private:
    uart_port_t uart_num_ = UART_NUM_2;
    TaskHandle_t uart_listener_task_handle_ = nullptr;
    bool uart_ok_ = false;

    void InitializeUart() {
        const uart_config_t uart_config = {
            .baud_rate = 9600,
            .data_bits = UART_DATA_8_BITS,
            .parity = UART_PARITY_DISABLE,
            .stop_bits = UART_STOP_BITS_1,
            .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
            .rx_flow_ctrl_thresh = 0,
            .source_clk = UART_SCLK_DEFAULT,
        };
        esp_err_t ret;
        ret = uart_driver_install(uart_num_, 1024, 0, 0, NULL, 0);
        ESP_LOGI(TAG, "uart_driver_install: %d", ret);
        if (ret != ESP_OK) { ESP_LOGE(TAG, "uart_driver_install failed: %s", esp_err_to_name(ret)); return; }
        ret = uart_param_config(uart_num_, &uart_config);
        ESP_LOGI(TAG, "uart_param_config: %d", ret);
        if (ret != ESP_OK) { ESP_LOGE(TAG, "uart_param_config failed: %s", esp_err_to_name(ret)); return; }
        ret = uart_set_pin(uart_num_, GPIO_NUM_17, GPIO_NUM_18, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
        ESP_LOGI(TAG, "uart_set_pin: %d", ret);
        if (ret != ESP_OK) { ESP_LOGE(TAG, "uart_set_pin failed: %s", esp_err_to_name(ret)); return; }
        uart_ok_ = true;
    }

    void SendIrCommand(const std::string &command) {
        if (!uart_ok_) {
            ESP_LOGE(TAG, "UART not initialized, cannot send IR command");
            return;
        }
        ESP_LOGI(TAG, "Sending IR command: %s", command.c_str());
        uart_write_bytes(uart_num_, command.c_str(), command.length());
    }

    std::string ReadFromUart() {
        if (!uart_ok_) return "";
        uint8_t data[1024];
        int len = uart_read_bytes(uart_num_, data, sizeof(data), pdMS_TO_TICKS(1000));
        if (len > 0) {
            return std::string(reinterpret_cast<char *>(data), len);
        }
        return "";
    }

    static void UartListenerTask(void *pvParameters) {
        InfraredTool *infrared_instance = reinterpret_cast<InfraredTool *>(pvParameters);
        while (infrared_instance->uart_ok_) {
            std::string response = infrared_instance->ReadFromUart();
            if (!response.empty()) {
                ESP_LOGI(TAG, "Received serial data: %s", response.c_str());
            }
        }
        vTaskDelete(NULL);
    }

    void StartUartListenerTask() {
        if (!uart_ok_) {
            ESP_LOGE(TAG, "UART not initialized, listener task not started!");
            return;
        }
        BaseType_t xReturned = xTaskCreate(
            UartListenerTask,
            "UartListenerTask",
            4096,
            this,
            tskIDLE_PRIORITY + 1,
            &uart_listener_task_handle_);
        if (xReturned != pdPASS) {
            ESP_LOGE(TAG, "Failed to create UART listener task");
        }
    }

public:
    static InfraredTool& GetInstance() {
        static InfraredTool instance;
        return instance;
    }
    InfraredTool() : McpTool("self.infrared.control", "MOSS红外遥控控制能力即红外遥控器，用于控制空调和房间灯光和学习红外指令") {
        InitializeUart();
        StartUartListenerTask();
    }

    void Register() override {
        ESP_LOGI(TAG, "注册红外遥控工具");
        McpServer::GetInstance().AddTool(
            name(),
            "MOSS红外遥控控制能力，支持以下操作（action）及参数（command）：\n"
            "1. 发送自定义红外指令：action=\"send_ir\"，command=\"xx01\"（自定义指令，字母需小写）；如需进入学习模式，command=\"xx00\"，学习到的值会通过终端输出。\n"
            "2. 房间灯光控制：action=\"room_light\"，command=\"turnOn\"（开灯）或 command=\"turnOff\"（关灯）。\n"
            "3. 空调控制：action=\"air_condition\"，command 可选：\"powerOn\"（开机）、\"powerOff\"（关机）、\"cool\"（制冷）、\"hot\"（制热）、\"dry\"（除湿）、\"16\"~\"32\"（温度，字符串数字）。\n"
            "4. 获取红外遥控器状态：action=\"get_status\"，无需 command 参数。\n"
            "注意：所有参数区分大小写，command 必须为字符串类型。\n"
            ,
            PropertyList({
                Property("action", kPropertyTypeString),
                Property("command", kPropertyTypeString, "")
            }),
            [this](const PropertyList& properties) -> ReturnValue {
                auto action = properties["action"].value<std::string>();
                auto command = properties["command"].value<std::string>();

                ESP_LOGI(TAG, "红外控制参数: command=%s, action=%s", command.c_str(), action.c_str());

                if (action == "send_ir") {
                    std::transform(command.begin(), command.end(), command.begin(), [](unsigned char c){ return std::tolower(c); });
                    if (!command.empty()) {
                        SendIrCommand(command);
                        return "已发送自定义红外指令";
                    } else {
                        return "缺少command参数";
                    }
                } else if (action == "room_light") {
                    ESP_LOGI(TAG, "room_light 查表前: command=%s", command.c_str());
                    auto ir_command = RoomLightMap[command];
                    ESP_LOGI(TAG, "room_light 查表后: ir_command=%s", ir_command.c_str());
                    if (!command.empty() && !ir_command.empty()) {
                        SendIrCommand("zf=" + ir_command);
                        return "已发送房间灯光指令";
                    } else {
                        ESP_LOGI(TAG, "无效的房间灯光指令: command=%s, ir_command=%s", command.c_str(), ir_command.c_str());
                        return "无效的房间灯光指令";
                    }
                } else if (action == "air_condition") {
                    auto ir_command = AirBtnMap[command];
                    if (!command.empty() && !ir_command.empty()) {
                        SendIrCommand("zf=" + ir_command);
                        return "已发送空调指令";
                    } else {
                        ESP_LOGI(TAG, "无效的空调指令: command=%s, ir_command=%s", command.c_str(), ir_command.c_str());
                        return "无效的空调指令";
                    }
                } else if (action == "get_status") {
                    return uart_ok_ ? "红外遥控器正常" : "UART初始化失败";
                } else {
                    return "未知动作: " + action + "\n支持的动作: send_ir, room_light, air_condition, get_status";
                }
            }
        );
    }
};

} // namespace mcp_tools

static auto& g_infrared_tool_instance = mcp_tools::InfraredTool::GetInstance();
DECLARE_MCP_TOOL_INSTANCE(g_infrared_tool_instance); 